package com.weige.conver.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.tools.json.JSONUtil;
import com.weige.conver.config.ActivitiConfig;
import com.weige.conver.entity.*;
import com.weige.conver.utils.DateUtils;
import com.weige.conver.utils.FileUtils;
import com.weige.conver.utils.JsonUtils;
import com.weige.conver.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

@Service
public class SwitchCabinetService {

    @Autowired
    private RabbitMQService rabbitMQService;
    @Autowired
    private DeviceService deviceService;
    private ConfigService configService = new ConfigService();

    /**
     * 上传开关柜的数据
     *
     * @return
     * @throws IOException
     */
    public void sendSwitchCabinetDataToMQTT(String message) throws IOException, InterruptedException {
        Map siteMap = configService.getResouceData();
        if (siteMap.isEmpty()){
            siteMap = FileUtils.getBaseMap();
        }
        JSONObject object = JSONObject.parseObject(message);
        String swiID = object.getString("swiID");
        String deviceId = "GPZ--"+swiID;
        String swiName = object.getString("swiName");
        String deviceName = "(光谱站-开关柜)："+swiName;
        JSONArray swPoints = object.getJSONArray("swPoints");
        List<Sig> points = new ArrayList<>();
        for (int i = 0; i < swPoints.size(); i++) {
            JSONObject point = swPoints.getJSONObject(i);
            String sigId = point.getString("sigId");
            String sigName = point.getString("sigName");
            String sigValue = point.getString("sigValue");
            String sigDescription = point.getString("sigDesc");
            Sig sig = new Sig();
            sig.setSigId(sigId);
            sig.setSigName(sigName);
            sig.setSigValue(sigValue);
            sig.setSigDescription(sigDescription);
            sig.setSigType("");
            sig.setSigUnit("");
            points.add(sig);
        }
        PosInfo posInfo = new PosInfo();
        posInfo.setSiteId((String) siteMap.get("siteId"));
        posInfo.setSiteName((String) siteMap.get("siteName"));
        posInfo.setObjectId(deviceId);
        posInfo.setObjectName(deviceName);
        posInfo.setObjectType("开关柜");
        posInfo.setMonitorDate(DateUtils.getNowTime());
        posInfo.setPoints(points);
        List<PosInfo> posInfoList = new ArrayList<>();
        posInfoList.add(posInfo);
        SwitchCabinetData switchCabinetData = new SwitchCabinetData();
        switchCabinetData.setSwitchCabinetDataList(posInfoList);
        switchCabinetData.setTopicName("SwitchMonitor_switchCabinetDataService");
        if (switchCabinetData != null) {
            String data = JSONObject.toJSONString(switchCabinetData);
            rabbitMQService.sendData(data, deviceId, deviceName, "switch_cabinet", "ShuyanyuanAIGateway", "SwitchMonitor_switchCabinetDataService");
        }
    }

    /**
     * 上传开关柜诊断报告结果
     *
     * @param message
     */
    public void sendSwitchCabinetDiagnoseResultDataToMQTT(String message) throws IOException {
        Map siteMap = configService.getResouceData();
        if (siteMap.isEmpty()){
            siteMap = FileUtils.getBaseMap();
        }
        JSONObject objectData = JSONObject.parseObject(message);
        JSONObject object = objectData.getJSONObject("param");
        JSONObject result = new JSONObject();
        String newIPv4 = object.getString("IPv4");
        String deviceId = "swdr_" + newIPv4.replace(".", "");
        String hostName = object.getString("hostName");
        String deviceName = object.getString("deviceName");
        String devName = hostName + "_" + deviceName;
        result.put("siteName", siteMap.get("siteId"));
        result.put("siteId", siteMap.get("siteId"));
        result.put("objectId", deviceId);
        result.put("objectName", devName);
        result.put("objectType", object.getString("actionType"));
        result.put("monitorDate", object.getString(DateUtils.getNowTime()));
        result.put("dataFileName", object.getString("datFileName"));
        result.put("isError", object.getInteger("isError"));
        result.put("strategy", object.getString("strategy"));
        if (object.containsKey("reason")) {
            result.put("reason", object.getString("reason"));
        }
        if (object.containsKey("remark")) {
            result.put("remark", object.getString("remark"));
        }
        result.put("topicName", "SwitchMonitor_switchCabinetDiagnoseResultService");
        if (result != null) {
            String data = JSONObject.toJSONString(result);
            rabbitMQService.sendData(data, deviceId, deviceName, "switch_cabinet", "ShuyanyuanAIGateway", "SwitchMonitor_switchCabinetDiagnoseResultService");
        }
    }

    /**
     * 10s往物联网平台推送开关柜主控设备数据
     */
//    @Scheduled(fixedRate = 10000)
//    public void sendMainSwitchCabinetDeviceToMQ() throws IOException {
//        Map siteMap = FileUtils.getBaseMap();
//        SwitchCabinetData switchCabinetData = new SwitchCabinetData();
//        List<PosInfo> posInfoList = new ArrayList<>();
//        PosInfo posInfo = new PosInfo();
//        posInfo.setSiteId((String) siteMap.get("siteId"));
//        posInfo.setSiteName((String) siteMap.get("siteName"));
//        posInfo.setObjectId("gzj_lqz_switchcabinet_master");
//        posInfo.setObjectName("110kV光谱变电站智能网关开关柜主控设备");
//        posInfo.setMonitorDate(DateUtils.getNowTime());
//        posInfo.setObjectType("switch_cabinet");
//        posInfoList.add(posInfo);
//        switchCabinetData.setSwitchCabinetDataList(posInfoList);
//        switchCabinetData.setTopicName("");
//        if (switchCabinetData != null) {
//            String data = JSONObject.toJSONString(switchCabinetData);
//            rabbitMQService.sendData(data, "gzj_gpz_switchcabinet_master", "110kV光谱变电站智能网关开关柜主控设备", "env_master", "ShuyanyuanAIGateway", "SwitchMonitor_switchCabinetDataService");
//        }
//    }

    /**
     * 获取开关柜设备列表
     *
     * @return
     * @throws IOException
     */
    public String getAllSwitchCabinetDevices() throws IOException {
        Map siteMap = configService.getResouceData();
        if (siteMap.isEmpty()){
            siteMap = FileUtils.getBaseMap();
        }
        Map<String, ReceiveMessage> map = ConverService.switchCabinetDataMap;
        List<EnvObject> envObjectList = deviceService.getDataList(map);
        JSONObject result = new JSONObject();
        result.put("returnCode", 200);
        result.put("returnMsg", "开关柜类设备列表");
        result.put("siteId", siteMap.get("siteId"));
        result.put("siteName", siteMap.get("siteName"));
        result.put("objectList", envObjectList);
        return JSONObject.toJSONString(result);
    }
}
